package cn.iocoder.yudao.module.biz.service.htHiddentroubleflow;

import cn.iocoder.yudao.module.biz.controller.admin.htHiddentrouble.vo.HtHiddentroublePageReqVO;
import cn.iocoder.yudao.module.biz.dal.dataobject.htHiddentrouble.HtHiddentroubleDO;
import cn.iocoder.yudao.module.biz.dal.dataobject.htHiddentroubleannexs.HtHiddentroubleannexsDO;
import cn.iocoder.yudao.module.biz.dal.mysql.htHiddentrouble.HtHiddentroubleMapper;
import cn.iocoder.yudao.module.biz.dal.mysql.htHiddentroubleannexs.HtHiddentroubleannexsMapper;
import cn.iocoder.yudao.module.infra.dal.dataobject.oss.OssDO;
import cn.iocoder.yudao.module.infra.dal.mysql.oss.OssMapper;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import cn.iocoder.yudao.module.biz.controller.admin.htHiddentroubleflow.vo.*;
import cn.iocoder.yudao.module.biz.dal.dataobject.htHiddentroubleflow.HtHiddentroubleflowDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;

import cn.iocoder.yudao.module.biz.convert.htHiddentroubleflow.HtHiddentroubleflowConvert;
import cn.iocoder.yudao.module.biz.dal.mysql.htHiddentroubleflow.HtHiddentroubleflowMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.biz.enums.ErrorCodeConstants.*;

/**
 * 隐患流程信息 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class HtHiddentroubleflowServiceImpl implements HtHiddentroubleflowService {

    @Resource
    private HtHiddentroubleflowMapper htHiddentroubleflowMapper;
    @Resource
    private HtHiddentroubleMapper htHiddentroubleMapper;
    @Resource
    private HtHiddentroubleannexsMapper htHiddentroubleannexsMapper;
    @Resource
    private OssMapper ossMapper;

    @Override
    public String createHtHiddentroubleflow(HtHiddentroubleflowCreateReqVO createReqVO) {
        // 插入
        HtHiddentroubleflowDO htHiddentroubleflow = HtHiddentroubleflowConvert.INSTANCE.convert(createReqVO);
        htHiddentroubleflowMapper.insert(htHiddentroubleflow);
        // 返回
        return htHiddentroubleflow.getId();
    }

    @Override
    public void updateHtHiddentroubleflow(HtHiddentroubleflowUpdateReqVO updateReqVO) {
        // 校验存在
        this.validateHtHiddentroubleflowExists(updateReqVO.getId());

        List<OssDO> ossList=updateReqVO.getOssFile();
        //批量插入隐患附件表信息
        for(OssDO ossDO:ossList){
            Long ossId=ossDO.getId();
            //查询文件ID是否已存在
            int isFileExist=htHiddentroubleMapper.isFileExist(updateReqVO.getHtId(),ossId);
            if(isFileExist>0){
                continue;
            }
            HtHiddentroubleannexsDO entity=new HtHiddentroubleannexsDO();
            entity.setHtId(updateReqVO.getHtId());
            entity.setOssId(ossId);
            entity.setIsImg(0);
            entity.setHtStep(updateReqVO.getStep());
            entity.setCreateTime(ossDO.getCreateTime());
            entity.setCreator(ossDO.getCreator());
            entity.setUpdateTime(ossDO.getUpdateTime());
            entity.setUpdater(ossDO.getUpdater());
            htHiddentroubleannexsMapper.insert(entity);
        }

        List<OssDO> imgList=updateReqVO.getRectificationOssImg();
        //批量插入隐患图片表信息
        for(OssDO ossDO:imgList){
            Long ossId=ossDO.getId();
            //查询文件ID是否已存在
            int isFileExist=htHiddentroubleMapper.isFileExist(updateReqVO.getHtId(),ossId);
            if(isFileExist>0){
                continue;
            }
            HtHiddentroubleannexsDO entity=new HtHiddentroubleannexsDO();
            entity.setHtId(updateReqVO.getHtId());
            entity.setOssId(ossId);
            entity.setIsImg(1);
            entity.setHtStep(updateReqVO.getStep());
            entity.setCreateTime(ossDO.getCreateTime());
            entity.setCreator(ossDO.getCreator());
            entity.setUpdateTime(ossDO.getUpdateTime());
            entity.setUpdater(ossDO.getUpdater());
            htHiddentroubleannexsMapper.insert(entity);
        }

        // 更新
        HtHiddentroubleflowDO updateObj = HtHiddentroubleflowConvert.INSTANCE.convert(updateReqVO);
        htHiddentroubleflowMapper.updateById(updateObj);

        HtHiddentroubleDO hiddentroubleDo=new HtHiddentroubleDO();
        hiddentroubleDo.setId(updateObj.getHtId());
        hiddentroubleDo.setState(updateReqVO.getHtState());
        htHiddentroubleMapper.updateById(hiddentroubleDo);
    }

    @Override
    public void deleteHtHiddentroubleflow(String id) {
        // 校验存在
        this.validateHtHiddentroubleflowExists(id);
        // 删除
        htHiddentroubleflowMapper.deleteById(id);
    }

    private void validateHtHiddentroubleflowExists(String id) {
        if (htHiddentroubleflowMapper.selectById(id) == null) {
            throw exception(HT_HIDDENTROUBLEFLOW_NOT_EXISTS);
        }
    }

    @Override
    public HtHiddentroubleflowDO getHtHiddentroubleflow(String id) {
        return htHiddentroubleflowMapper.selectById(id);
    }

    @Override
    public HiddentroubleflowJoinVO queryById(String id,int step) {
        HiddentroubleflowJoinVO hiddentroubleflowJoinVO = htHiddentroubleflowMapper.queryById(id);
        if (null != hiddentroubleflowJoinVO) {
//            int step = Integer.getInteger(hiddentroubleflowJoinVO.getState());
            //TODO 根据附件、图片ID 查询
            Map params = new HashMap();
            params.put("id", id);
            params.put("step", step);
            params.put("isImg", 0);

        List<String> ossFileIds = htHiddentroubleannexsMapper.getFileByHtId(params);
        if (!ossFileIds.isEmpty()) {
            List<OssDO> ossFiles = ossMapper.selectByIds(ossFileIds);
            hiddentroubleflowJoinVO.setOssFile(ossFiles);
        }

        //查询整改后的图片
        params.put("isImg", 1);
        params.put("step", "2");
        List<String> ossImgIds = htHiddentroubleannexsMapper.getFileByHtId(params);
        if (!ossImgIds.isEmpty()) {
            List<OssDO> ossImgs = ossMapper.selectByIds(ossImgIds);
            hiddentroubleflowJoinVO.setRectificationOssImg(ossImgs);
        }

        params.put("step", "0");
        //TODO 查询整改前的图片
        List<String> beforeImgIds = htHiddentroubleannexsMapper.getFileByHtId(params);
            if (!beforeImgIds.isEmpty()) {
                List<OssDO> ossImgs = ossMapper.selectByIds(beforeImgIds);
                hiddentroubleflowJoinVO.setOssImg(ossImgs);
            }
    }
        return hiddentroubleflowJoinVO;
    }

    @Override
    public List<HiddentroubleflowPageJoinVO> selectHiddentroublePage(HtHiddentroubleflowPageReqVO pageVO) {
        return htHiddentroubleflowMapper.selectHiddentroublePage(pageVO);
    }

    @Override
    public Integer selectHiddentroubleCount(HtHiddentroubleflowPageReqVO pageVO) {
        return htHiddentroubleflowMapper.selectHiddentroubleCount(pageVO);
    }

    @Override
    public List<HtHiddentroubleflowDO> getHtHiddentroubleflowList(Collection<String> ids) {
        return htHiddentroubleflowMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<HtHiddentroubleflowDO> getHtHiddentroubleflowPage(HtHiddentroubleflowPageReqVO pageReqVO) {
        return htHiddentroubleflowMapper.selectPage(pageReqVO);
    }

    @Override
    public List<HtHiddentroubleflowDO> getHtHiddentroubleflowList(HtHiddentroubleflowExportReqVO exportReqVO) {
        return htHiddentroubleflowMapper.selectList(exportReqVO);
    }

    @Override
    public List<HtHiddentroubleflowExcelVO> getHtHiddentroubleExcelList(HtHiddentroubleflowExportReqVO exportReqVO) {
        return htHiddentroubleflowMapper.getHtHiddentroubleExcelList(exportReqVO);
    }
}
